package com.pervasive.geoTagging;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    private ArrayList <Drawable> arr ;

    public ImageAdapter(Context c) {
    	
		
    	arr= new ArrayList<Drawable>();
    	Drawable in=null;
    	
    	File f;
    	
    	
    	for(int i=CustomGalleryActivity.getNext(); i<(3+CustomGalleryActivity.getNext()); i++)
    	{
    		in=null;
    		//
    		try{
    			
    			if((f = new File(Environment.getExternalStorageDirectory().toString()+"/geoTagging folder/photos/"+i+"perv.jpeg")).exists())
    			{
    				
    				in=  Drawable.createFromPath(Environment.getExternalStorageDirectory().toString()+"/geoTagging folder/photos/"+i+"perv.jpeg");
    				if(i==CustomGalleryActivity.getTarget())
    				{
    						
    					Bitmap bmp = ((BitmapDrawable)in).getBitmap();
    					// Getting width & height of the given image.
    					int w = bmp.getWidth();
    					int h = bmp.getHeight();
    					// Setting post rotate to 90
    					Matrix mtx = new Matrix();
    					mtx.postRotate(90);
    					// Rotating Bitmap
    					Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
    					BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
    					in=bmd;
    					File g = new File(Environment.getExternalStorageDirectory().toString()+"/geoTagging folder/photos/"+i+"perv.jpeg");
    					FileOutputStream out=new FileOutputStream(g);
    					//
    					
    						try {
    							
    							rotatedBMP.compress(Bitmap.CompressFormat.PNG, 100, out);    							
    							out.flush();
    						    out.close();

    						} catch (Exception e) {
    						       e.printStackTrace();
    						}

    						
    					CustomGalleryActivity.setTarget(0);
    				}
    				arr.add(in);
    				
    			}	
    			
    		
			
    		}
    		
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		  
    	}
    	
        mContext = c;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground, 0);
        attr.recycle();
    }

    public int getCount() {
        return arr.size();
    }

    public Object getItem(int position) {
        return arr.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    	ImageView imageView;
        if (convertView == null)
            imageView = new ImageView(mContext);
        else
            imageView = (ImageView)convertView;
        
        Bitmap bmp = ((BitmapDrawable)arr.get(position)).getBitmap();
		BitmapDrawable bmd = new BitmapDrawable(Bitmap.createScaledBitmap(bmp, 400,320, false));
		
        imageView.setImageDrawable(bmd);
        imageView.setLayoutParams(new Gallery.LayoutParams(400,400 ));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        return imageView;
    }
    
    public void remove(int position){
    	
    	arr.remove(position);
    	
    	
    }
    
}